package cn.itcast.order.HandlerInterceptor;

import cn.itcast.hmall.dto.common.ThreadLocalUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author mrchen
 * @date 2022/6/19 20:29
 */
@Component
@WebFilter(value = "authorFilter",urlPatterns = "/*")
public class AuthorFilter extends GenericFilter
{
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        String authorization = request.getHeader("authorization");
        if (StringUtils.isNotBlank(authorization)) {
            // 将请求头中的用户id 设置到ThreadLocal中
            ThreadLocalUtil.setUserId(Long.valueOf(authorization));
        }
        filterChain.doFilter(servletRequest,servletResponse);
        // 请求登陆信息
        ThreadLocalUtil.clear();
    }
}
